package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.enums.LogType;
import com.tanhua.dubbo.server.pojo.Managelogs;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@Service(version = "1.0.0")
public class ManageLogsApiImpl implements ManageLogsApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 添加日志到MongoDB中
     *
     * @param userId
     * @param place
     * @param equipment
     * @param logTime
     * @return
     */
    @Override
    public Boolean saveLogin(Long userId, String place, String equipment, Long logTime) {

        if (ObjectUtil.isEmpty(userId)) {
            return false;
        }
        Managelogs managelogs = new Managelogs();
        managelogs.setId(new ObjectId());
        managelogs.setUserId(userId);
        managelogs.setLogTime(logTime);
        managelogs.setEquipment(equipment);
        managelogs.setType(LogType.LOGIN.getType());

        managelogs.setPlace(place);
        mongoTemplate.save(managelogs);
        return true;
    }

    /**
     * 查询日志
     *
     * @param page
     * @param pageSize
     * @param sortProp
     * @param sortOrder
     * @param uid
     * @param login
     * @return
     */
    @Override
    public PageInfo<Managelogs> queryLoginLogList(Integer page, Integer pageSize, String sortProp, String sortOrder, Long uid, LogType login) {

        PageInfo<Managelogs> pageInfo = new PageInfo();
        if (uid == null) {
            return pageInfo;
        }
        Sort sort = null;

        //判断字段与升序(降序)是否为空
        if (!StringUtils.isAnyEmpty(sortProp, sortOrder)) {

            //不为空则判断sortOrder是升序或降序
            if (StringUtils.equals(sortOrder, "ascending")) {
                sort = Sort.by(Sort.Order.asc(sortProp));
            } else if (StringUtils.equals(sortOrder, "descending ")) {
                sort = Sort.by(Sort.Order.asc(sortProp));
            } else {
                //若没有传递参数则按照默认排序规则
                sort = Sort.by(Sort.Order.desc("loginTime"));
            }
        }
        PageRequest pageRequest = new PageRequest(page - 1, pageSize, sort);

        //设置查询条件
        Query query = Query.query(Criteria.where("userId").is(uid).and("type").is(login.getType())).with(pageRequest);
        pageInfo.setRecords(mongoTemplate.find(query, Managelogs.class));


        //
        Query query1 = Query.query(Criteria.where("userId").is(uid).and("type").is(login.getType()));
        List<Managelogs> managelogsList = mongoTemplate.find(query1, Managelogs.class);
        pageInfo.setTotal(managelogsList.size());


        return pageInfo;
    }

}
